package cn.buk.api.szkj.service;

import cn.buk.api.szkj.dto.FlightInfo;
import cn.buk.api.szkj.dto.FlightSearchResult;
import cn.buk.common.util.DateUtil;
import cn.buk.common.util.EncryptUtil;
import cn.buk.common.util.HttpUtil;
import cn.buk.common.util.JsonUtil;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.hc.core5.http.NameValuePair;
import org.apache.hc.core5.http.message.BasicNameValuePair;

import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

import static cn.buk.common.util.EncryptUtil.Base64Encoding;


public class SzkjFlightServiceImpl extends AbstractServiceImpl implements SzkjFlightService {

  public SzkjFlightServiceImpl(String username, String apiUrl) {
    super(username, apiUrl);
  }

  private class FlightRequest {
    @JsonProperty(value = "fromcity")
    private String dcity;

    @JsonProperty(value = "tocity")
    private String acity;

    @JsonProperty(value = "fromdate")
    private String ddate;

    private int isPriceAll = 0;

    private String shareaFlights = "1";

    public FlightRequest(String dcity, String acity, String ddate) {
      this.dcity = dcity;
      this.acity = acity;
      this.ddate = ddate;
    }

    public String getDcity() {
      return dcity;
    }

    public void setDcity(String dcity) {
      this.dcity = dcity;
    }

    public String getAcity() {
      return acity;
    }

    public void setAcity(String acity) {
      this.acity = acity;
    }

    public String getDdate() {
      return ddate;
    }

    public void setDdate(String ddate) {
      this.ddate = ddate;
    }

    public int getIsPriceAll() {
      return isPriceAll;
    }

    public void setIsPriceAll(int isPriceAll) {
      this.isPriceAll = isPriceAll;
    }

    public String getShareaFlights() {
      return shareaFlights;
    }

    public void setShareaFlights(String shareaFlights) {
      this.shareaFlights = shareaFlights;
    }
  }

  @Override
  public FlightSearchResult searchFlights(String dcity, String acity, Date ddate) {
    final String url = this.apiUrl + "/app/api/interface.php";

    List<NameValuePair> params = new ArrayList<>();
    params.add(new BasicNameValuePair("fun_name", "shoping_policy"));
    params.add(new BasicNameValuePair("user_name",  this.username));

    FlightRequest request = new FlightRequest(dcity, acity, DateUtil.formatDate(ddate, "yyyy-MM-dd"));

    String requestStr = JsonUtil.toJSONString(request);

//    shareaFlights String 共享航班 N 1 - 返回共享航班 0 - 不返回共享航班
//    carrierCode String 航空公司 N 航司二字码，改签查询时用，如：MU-FM；注：国内航司中，东上航包括MU、FM2个航司代码，改签查询时如是东上航航班，请求需提交2个航司
//    isPriceAll Integer 舱位价格 Y 是否返回全部舱位价格 0 - 返回最低价格 1 - 返回所有舱位价格
    String base64Request = null;
    try {
      base64Request = Base64Encoding(requestStr);
    } catch (NoSuchAlgorithmException e) {
      e.printStackTrace();
    }

    params.add(new BasicNameValuePair("request", base64Request));

    String jsonStr = HttpUtil.postUrl(url, params);

    jsonStr = EncryptUtil.decodeUnicode(jsonStr);

    try {
      List<FlightInfo> flightInfos = Arrays.asList(JsonUtil.convertJson2Obj(jsonStr, FlightInfo[].class));
      FlightSearchResult result = new FlightSearchResult();
      result.setFlights(flightInfos);
      return result;
    } catch (Exception ex) {
      return JsonUtil.convertJson2Obj(jsonStr, FlightSearchResult.class);
    }
  }
}
